<template>
    <div>
        <el-dialog title="添加职工信息" :visible.sync="is_show" width="50%" :before-close="handleClose">
            <el-form label-position="right" label-width="80px" :model="user_form" ref="user_form" :rules="rules" class="personnel-add-form">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="user_form.name"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="user_form.mobile"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="handleClose('user_form')">取 消</el-button>
                <el-button type="primary" @click="submitForm('user_form')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import user from "@/api/user";

export default {
    name: "add-user",
    computed: {
        ...mapGetters("personnel", ["is_add_user_dialog_show"])
    },
    data() {
        return {
            user_form: {
                name: undefined,
                mobile: undefined
            },
            rules: {
                name: [
                    {
                        required: true,
                        trigger: "blur",
                        message: "请输入姓名"
                    }
                ],
                mobile: [
                    {
                        required: true,
                        trigger: "blur",
                        message: '请输入手机号'
                    }
                ]
            },
            is_show: false
        };
    },
    methods: {
        handleClose(formName) {
            this.user_form = {
                name: undefined,
                mobile: undefined
            };
            this.$store.commit(
                "personnel/CHANGE_IS_ADD_USER_DIALOG_SHOW",
                false
            );
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    user.createUser(this.user_form).then(res => {
                        this.$message({
                            type: "success",
                            message: "添加成功!"
                        });
                        this.$store.commit(
                            "personnel/CHANGE_IS_ADD_USER_DIALOG_SHOW",
                            false
                        );
                        user
                            .getTableData({pageNumber: 1})
                            .then(res => {
                                this.$store.commit(
                                    "personnel/CHANGE_USER_LIST",
                                    res
                                );
                            })
                            .catch(err => {
                                this.$message.error("刷新失败，请手动刷新!");
                            });
                        this.$refs[formName].resetFields();
                    }).catch(err => {
                        this.$message.error(err);
                        this.$store.commit(
                            "personnel/CHANGE_IS_ADD_USER_DIALOG_SHOW",
                            false
                        );
                        this.$refs[formName].resetFields();
                    })
                } else {
                    return false;
                }
            });
        }
    },
    watch: {
        is_add_user_dialog_show(newVal) {
            this.is_show = newVal;
        }
    }
};
</script>

<style>
.personnel-add-form {
    width: 50%;
    margin: 0 auto;
}
</style>
